package edu.fudan.cse.medlab.event.crawler;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Hashtable;

import edu.fudan.cse.medlab.event.extraction.Main;
import edu.fudan.cse.medlab.event.filter.DataBase;
import edu.fudan.cse.medlab.event.filter.FilterByPrefix;
import edu.fudan.cse.medlab.event.filter.Settings;

public class GenerateNavigator extends Thread{
	String sitename;
	String index;
	String folder;
	Settings setting;
	
	public GenerateNavigator(String sitename,Settings setting,String folder){
		this.sitename = sitename;
		this.folder = folder;
		if(!this.folder.endsWith("\\"))
			this.folder = this.folder+"\\";
		this.setting = setting;

	}
	
	
	
	public static String[] getDistinctSite(String prefixfile,String sitefile) throws Exception{
		BufferedReader bin = new BufferedReader(new FileReader(prefixfile));
		BufferedWriter bout = new BufferedWriter(new FileWriter(sitefile));
		String s,temp = "";
		while( (s = bin.readLine()) != null){
			if(s.contains("/"))
				s = s.substring(0,s.indexOf("/"));
			if(!temp.contains(s)){
				temp += s + "|";
				bout.write(s+"\r\n");
				bout.flush();
			}
		}
		bin.close();
		bout.close();
		if(temp.contains("|")){
			temp.substring(0,temp.length()-1);
			return temp.split("\\|");
		}
		return null;
	}
	
	public void run(){
		try{
			long t1 =System.currentTimeMillis();
			Navigator na = new Navigator(sitename);
			generateNavigator();
			na.readNavigator(setting.folder+"Navigator\\navigator("+sitename+")");	
			long t2 = System.currentTimeMillis();
			System.out.println("time used:  " + (t2-t1)+"ms");
			edu.fudan.cse.medlab.event.extraction.DB.addLog("Finish Site:" +sitename+"   time used:  " + (t2-t1)+"ms");
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	public void generateNavigator() throws Exception{
		Navigator na = new Navigator(sitename);
		na.readSiteMap(setting.folder+"SiteMap\\sitemap("+sitename+")");

		na.readTargetPage("prefix(final).txt");
		System.out.println(sitename+"    total page:"+na.countPage(na.sitehead));
		edu.fudan.cse.medlab.event.extraction.DB.addLog(sitename+"    total page:"+na.countPage(na.sitehead));

		na.generateTargetPageMap();
		System.out.println(sitename +"     target page:"+na.countPage(na.sitehead));
		edu.fudan.cse.medlab.event.extraction.DB.addLog(sitename +"     target page:"+na.countPage(na.sitehead));

		
		na.saveTargetPageMap(setting.folder+"TargetPageMap\\targetpagemap("+sitename+")");
		na.generateNavigator();
		na.saveNavigator(setting.folder+"Navigator\\navigator("+sitename+")");
		edu.fudan.cse.medlab.event.extraction.DB.addLog("nabigator generated for site:  "+sitename);
	}
	
	
	public static void main(String[] args){
		try{
			Settings setting = new Settings();
			String[] sites = getDistinctSite("prefix(final).txt","finalsite.txt");
			if(sites==null){
				edu.fudan.cse.medlab.event.extraction.DB.addLog("no site!!");
				return;
			}
			
			String sitefile = "finalsite.txt";
			String blacklist = "blacklist.txt";
			
			String[] sitename = GetCandidateSite.getFromSiteFile(sitefile,blacklist);
			GenerateNavigator[] fna = new GenerateNavigator[sitename.length];
			
			edu.fudan.cse.medlab.event.extraction.DB.addLog("Crawler Start");
			
			for (int i = 0; i < fna.length; i++) {
				fna[i] = new GenerateNavigator(sitename[i],setting,setting.folder+"page\\finalsite");
				fna[i].start();
			}
			for (int i = 0; i < fna.length; i++) {
	            fna[i].join();
	        }			 
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
}
